/*
 * Copyright 2016-2021 the original author.All rights reserved.
 * Kingstar(aiteasoft@163.com)
 * The license,see the LICENSE file.
 */

package com.aiteasoft.school.admin.rest;

import static com.automvc.common.service.CommService.hasNull;
import static com.automvc.common.service.CommService.isNullPara;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.teasoft.bee.osql.service.ObjSQLRichService;
import org.teasoft.bee.osql.service.ObjSQLService;

import com.automvc.common.jq.PageWarp;
import com.automvc.common.jq.Result;

import com.aiteasoft.school.admin.entity.Course;
import com.aiteasoft.school.admin.service.CourseService;

/**
 * @author AiTeaSoft.com
 * @since  1.9
 * Create on 2021-02-07 18:18:20
 */
@RestController
@RequestMapping("course")
public class CourseRest {
	@Autowired
	ObjSQLService objSQLService;
	@Autowired
	ObjSQLRichService objSQLRichService;

	@Autowired
	CourseService courseService;

	@RequestMapping("/list")
	public Result list(Course course, 
			@RequestParam(value = "page", defaultValue = "1", required = false) int page,
			@RequestParam(value = "rows", defaultValue = "20", required = false) int rows) {

		Result result = new Result();
		if (isNullPara(result, course)) return result;

		int total = objSQLRichService.count(course);
		List<Course> list = objSQLRichService.select(course, (page - 1) * rows, rows);
		result.setRows(list);
		result.setTotal(total);

		return result;
	}
	
	
	@RequestMapping("/all")
	public Result findAll() {

		Result result = new Result();
		
		List<Course> list=objSQLService.select(new Course());
		
		result.setTotal(list.size());
		result.setRows(list);

		return result;
	}

	@RequestMapping("/add")
	public Result insert(Course course) {

		Result result = new Result();
		if (isNullPara(result, course)) return result;

		String checkFields = "subject,subjectno,";
		if (hasNull(result, course, checkFields)) return result;

//		course.setCreater(creater); //TODO 老师编号
		int num = objSQLService.insert(course);
		result.setTotal(num);
		if (num <= 0) result.setErrorMsg("insert failed!");
		return result;
	}

	@RequestMapping("/edit")
	public Result update(Course course) {

		Result result = new Result();
		if (isNullPara(result, course)) return result;
		if(courseService.isWrongBizForUpdate(result, course)) return result;

		String checkFields = "id,subject,subjectno,";
		if (hasNull(result, course, checkFields)) return result;
		
		course = courseService.process(course);
		
		int num = courseService.update(course);
		result.setTotal(num);
		if (num <= 0) result.setErrorMsg("update failed!");
		return result;
	}

	@RequestMapping("/del")
	public Result delete(String ids) {

		Result result = new Result();
		if (isNullPara(result, ids, "ids")) return result;

//		int num = objSQLRichService.deleteById(Course.class, ids);
		
		Course course=new Course();
		course.setId(Long.parseLong(ids));
		course.setStatus("0");  //del flag
		
		int num =objSQLRichService.update(course);
		
		result.setTotal(num);
		if (num <= 0) result.setErrorMsg("delete failed!");
		return result;
	}
}